package Work2;
import java.util.Random;
import java.util.Scanner;

public class Migong {
	public static void main(String[] args) {
		BeginGame();
	}
	
	public static int[][] CreateMaze() {
		Random rd=new Random();
		int[][] maze = new int[5][5];
		for(int i=0;i<maze.length;i++) {
			for(int j=0;j<maze[i].length;j++) {
				maze[i][j]=rd.nextInt(2);
			}
		}
		return maze;
	} //生成迷宫
	
	public static void Themaze(int[][] a) {
		for(int i=0;i<a.length;i++) {
			for(int j=0;j<a[i].length;j++) {
				System.out.printf("%3d",a[i][j]);
			}
			System.out.printf("\n");
		}
	} //打印迷宫
	
	public static int BeginGame() {
		Scanner sc=new Scanner(System.in);
		System.out.println("走迷宫");
		System.out.println("数字'5'代表当前位置，输入wasd进行移动");
		System.out.println("迷宫左上角为入口，右下角为出口");
		int[][] maze=CreateMaze();
		maze[0][0]=5;
		int x=0,y=0;
		System.out.println("---迷宫---");
		Themaze(maze);
		while(maze[maze.length-1][maze[0].length-1]!=5) {
			System.out.println("输入wasd进行移动,若要结束游戏则输入no");
			String action = sc.next();
			switch(action) {
				case "w":
					if(x==0||maze[x-1][y]==1) {
						System.out.println("无法向上移动，请再次输入");
						break;
					}
					maze[x][y]=0;
					maze[x-1][y]=5;
					x=x-1;
					System.out.println("---迷宫---");
					Themaze(maze);
					break;
				case "a":
					if(y==0||maze[x][y-1]==1) {
						System.out.println("无法向左移动，请再次输入");
						break;
					}
					maze[x][y]=0;
					maze[x][y-1]=5;
					y=y-1;
					System.out.println("---迷宫---");
					Themaze(maze);
					break;
				case "s":
					if(x==maze.length-1||maze[x+1][y]==1) {
						System.out.println("无法向下移动，请再次输入");
						break;
					}
					maze[x][y]=0;
					maze[x+1][y]=5;
					x=x+1;
					System.out.println("---迷宫---");
					Themaze(maze);
					break;
				case "d":
					if(y==maze[maze.length-1].length-1||maze[x][y+1]==1) {
						System.out.println("无法向右移动，请再次输入");
						break;
					}
					maze[x][y]=0;
					maze[x][y+1]=5;
					y=y+1;
					System.out.println("---迷宫---");
					Themaze(maze);
					break;
				case "no":
					maze[maze.length-1][maze[maze.length-1].length-1]=5;
					System.out.println("游戏结束");
					return 0;
				default:
					System.out.println("输入错误，请再次输入");
			}
		}
		System.out.println("成功！");
		return 0;
	} //游戏逻辑
}
